﻿using System;
using Handler.Business;
using System.Web.SessionState;
using Bright.Library.BO.Pub;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Common.WxPayAPI;
using Dln.Model;
using Dos.ORM;

namespace Dln.Handler
{
    /// <summary>
    /// 文本信息处理类
    /// </summary>
    public class TextHandler : IHandler
    {
        /// <summary>
        /// 请求的XML
        /// </summary>
        private string RequestXml { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="requestXml">请求的xml</param>
        public TextHandler(string requestXml)
        {
            this.RequestXml = requestXml;
        }

        ResponseMessage responseMessage = new ResponseMessage();
        /// <summary>
        /// 处理消息请求
        /// </summary>
        /// <returns></returns>
        public string HandleRequest()
        {
            //客户消息
            TextMessage clientMessage = TextMessage.LoadFromXml(RequestXml);
            responseMessage.FromUserName = clientMessage.ToUserName;
            responseMessage.ToUserName = clientMessage.FromUserName;
            responseMessage.ResponseType = ResponseType.Text;

            string content = clientMessage.Content.Trim();
            string openId = clientMessage.FromUserName;
            if (string.IsNullOrEmpty(content))
            {
                responseMessage.Content = "您什么都没输入，没法帮您啊，%>_<%。";
            }
            else
            {
                try
                {
                    string[] loginInfo = new string[3];
                    content = content.Replace("，", ",").Trim(',').Trim();
                    loginInfo = content.Split(',');
                    if (loginInfo.Length == 3)
                    {
                        string md5Password = SecurityHelper.Md5EncryptStr32(loginInfo[1].ToString());
                        var userModel =
                            SqlHelper.ContextDlnOffice.From<Users>()
                                .Where(d => d.UserId == loginInfo[0].ToString() && d.Password == md5Password)
                                .First();
                        if (userModel != null)
                        {
                            var wxUserModel =
                                SqlHelper.Context.From<WeChatUsers>()
                                    .Where(d => d.Openid == openId)
                                    .First();
                            if (wxUserModel != null)
                            {
                                wxUserModel.CardID = loginInfo[0].ToString();
                                wxUserModel.RoleType = 0;
                                var count = SqlHelper.Context.Update<WeChatUsers>(wxUserModel);

                                if (!string.IsNullOrEmpty(loginInfo[2]))
                                {
                                    userModel.IDNumber = loginInfo[2];
                                    if (BString.CheckIDCard(userModel.IDNumber) == false)
                                    {
                                        responseMessage.Content = "身份证号错误，请重新输入！";
                                    }
                                    else
                                    {
                                        var count1 = SqlHelper.ContextDlnOffice.Update<Users>(userModel);
                                        responseMessage.Content = count > 0 && count1 > 0 ? "用户信息更新成功！" : "用户信息更新失败,请重新输入或联系信息科！";
                                    }
                                }
                               
                            }
                            else
                            {
                                responseMessage.Content = "未查到改用户关注信息，请重新关注或联系信息中心！";
                            }
                        }
                        else
                        {
                            responseMessage.Content = "用户名或密码错误,请重新输入。格式：工号,密码,身份证号。例：1963,88888,321322********1111";
                        }
                    }
                    else
                    {
                        responseMessage.Content = "格式错误,请重新输入。格式：工号,密码,身份证号。例：1963,88888,321322********1111";
                    }
                }
                catch (Exception e)
                {
                    Log.Info(openId, "更新用户信息失败: " + e.ToString());
                    responseMessage.Content = e.ToString();
                    return responseMessage.ResponseTest;
                }

            }
            return responseMessage.ResponseTest;
        }


    }
}